//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.qst.crop.service.impl;

import com.qst.crop.dao.FinanceDao;
import com.qst.crop.entity.*;
import com.qst.crop.service.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class FinanceServiceImpl implements FinanceService {
    @Autowired
    private FinanceDao financeDao;

    public FinanceServiceImpl() {
    }

    public void add(Finance finance) {
        finance.setStatus(0);
        finance.setCreateTime(new Date());
        finance.setUpdateTime(new Date());
        this.financeDao.insertSelective(finance);
    }

    public List<Finance> selectByFinance(Finance finance) {
        List<Finance> finances = this.financeDao.selectByFinance(finance);
        return finances;
    }

    public Finance selectByFinanceId(Integer id) {
        Finance finance = this.financeDao.selectByPrimaryKey(id);
        return finance;
    }

    public void deleteByFinanceId(Integer id) {
        this.financeDao.deleteByPrimaryKey(id);
    }

    public void updateByFinance(Finance finance) {
        finance.setUpdateTime(new Date());
        this.financeDao.updateByPrimaryKeySelective(finance);
    }

    public List<Intention> selectIntentionByName(String name) {
        List<Intention> intentions = this.financeDao.selectIntentionByName(name);
        return intentions;
    }

    public void updateIntentionByName(Intention intention) {
        intention.setUpdateTime(new Date());
        this.financeDao.updateIntentionByName(intention);
    }

    public void insertIntentionByName(Intention intention) {
        intention.setCreateTime(new Date());
        intention.setUpdateTime(new Date());
        this.financeDao.insertIntentionByName(intention);
    }

    public void deleteIntentionByName(String name) {
        this.financeDao.deleteIntentionByName(name);
    }

    public List<Recommend> selectRecommend(String name) {
        Intention intention = this.financeDao.selectIntention(name);
        if (StringUtils.isEmpty(intention)) {
            List<Recommend> list = this.financeDao.selectWithNoneIntention(name);
            System.out.println("无意向：" + list);
            return list;
        } else {
            String item = intention.getItem();
            String address = intention.getAddress();
            int she = address.indexOf(30465);
            int shi = address.indexOf(24066);
            she = she == -1 ? 0 : she;
            shi = shi == -1 ? 0 : shi;
            address = address.substring(she + 1, shi);
            List<Recommend> list = this.financeDao.selectAllRecommend(name, item, address);
            System.out.println(list);
            return list;
        }
    }

    public void AuthorizationtoUser(String name, String userName) {
        this.financeDao.AuthorizationtoUser(name, userName);
    }

    public FinaceUserDetails selectFinaceUser(String name, String bankId) {
        FinaceUserDetails user = new FinaceUserDetails();
        Bank bank = this.financeDao.selectBankById(bankId);
        user.setRate(bank.getRate());
        user.setIntroduce(bank.getIntroduce());
        user.setBankName(bank.getBankName());
        System.out.println("user:" + user.toString());
        return user;
    }

    public boolean selectIfApply(String name) {
        Finance finance = this.financeDao.selectIfApply(name);
        return !StringUtils.isEmpty(finance);
    }

    public void addMulti(Finance finance) {
        finance.setCreateTime(new Date());
        finance.setUpdateTime(new Date());
        finance.setStatus(0);
        this.financeDao.insertMulti(finance);
    }
}
